//
//  ICMDatePickerView.m
//  ICrEAM
//
//  Created by rainimute on 16/11/8.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMDatePickerView.h"

@interface ICMDatePickerView ()

@property (nonatomic, strong) UIButton *cancelButton;

@property (nonatomic, strong) UIButton *confirmButton;



@end

@implementation ICMDatePickerView

- (instancetype)initWithFrame:(CGRect)frame minDate:(NSDate *)minDate maxDate:(NSDate *)maxDate andPredefinedDate:(NSDate *)preDate andDateModel:(NSInteger)dateModel{
    if (self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]) {
        self.backgroundColor = [UIColor clearColor];
        UIView *contentView = [[UIView alloc]initWithFrame:frame];
        contentView.backgroundColor = [UIColor whiteColor];
        
        //增加取消手势
        UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelSelect)];
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:tapGr];
        
        //
        self.cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
        [self.cancelButton setUpButtonWithNormolTitle:@"清空" selectedTitle:@"清空" normolTitleSize:16 selectedTitleSize:16 normolTitleColor:[UIColor redColor] selectedTitleColor:[UIColor redColor] normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(cancelButtonAction:) target:self];
        [contentView addSubview:self.cancelButton];
        //
        self.confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(frame)-CGRectGetWidth(self.cancelButton.frame), 0, CGRectGetWidth(self.cancelButton.frame), CGRectGetHeight(self.cancelButton.frame))];
        [self.confirmButton setUpButtonWithNormolTitle:@"确定" selectedTitle:@"确定" normolTitleSize:16 selectedTitleSize:16 normolTitleColor:[UIColor blackColor] selectedTitleColor:[UIColor blackColor] normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(confirmButtonAction:) target:self];
        [contentView addSubview:self.confirmButton];
        //
        self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.confirmButton.frame), CGRectGetWidth(frame), CGRectGetHeight(frame)-CGRectGetHeight(self.confirmButton.frame))];
        self.datePicker.datePickerMode = dateModel==0?UIDatePickerModeDate:UIDatePickerModeDateAndTime;
        self.datePicker.minimumDate = minDate;
        self.datePicker.maximumDate = maxDate;
        if (preDate) {
            [self.datePicker setDate:preDate animated:NO];
        }
        BorderWidth(self.datePicker, 1);
        BorderColor(self.datePicker, BorderColor_Gray_Hex);
        
        BorderWidth(contentView, 1);
        BorderColor(contentView, BorderColor_Gray_Hex);
        
        [contentView addSubview:self.datePicker];
        [self addSubview:contentView];
    }
    return self;
}

- (void)cancelSelect
{
    [self removeFromSuperview];
}

- (void)cancelButtonAction:(UIButton*)sender{
    //清空原来的时间
    if([self.delegate respondsToSelector:@selector(delTime:)])
    {
        [self.delegate delTime:self];
    }
    [self removeFromSuperview];
}
- (void)confirmButtonAction:(UIButton*)sender{
    NSDate *selectedDate = self.datePicker.date;
    if ([self.delegate respondsToSelector:@selector(datePickerView:didSelectedDate:)]) {
        [self.delegate datePickerView:self didSelectedDate:selectedDate];
    }
    [self removeFromSuperview];
}

+ (instancetype)createDatePickerViewWithFrame:(CGRect)frame minDate:(NSDate *)minDate maxDate:(NSDate *)maxDate andPredefinedDate:(NSDate *)preDate andDateModel:(NSInteger)dateModel{
    return [[ICMDatePickerView alloc]initWithFrame:frame minDate:minDate maxDate:maxDate andPredefinedDate:preDate andDateModel:dateModel];
}

@end
